<template>
  <div class="container">
    <!-- 标题 -->
    <div class="settitle">
      <div class="setimg" @click="$router.back()">
        <img src="../assets/imgs/iconse/左箭头.png" alt="" />
      </div>
      <div class="text"></div>
    </div>
    <div class="box">
      <h2>登录</h2>
    </div>
    <div class="form">
      <div class="item">
        <div class="phones code">
          <span>+86</span>
        </div>
        <input
          type="text"
          placeholder="手机号码"
          v-model="phone"
          @keyup.enter="submit"
        />
      </div>
      <div class="box">
        <div :class="['btn', flag ? '' : 'con']" @click="setcode">
          {{ text }}
        </div>
      </div>
      <div class="item">
        <div class="phones">
          <span>验证码</span>
        </div>
        <input
          type="text"
          placeholder="请输入验证码"
          v-model="code"
          @keyup.enter="submit"
        />
      </div>
      <div class="item">
        <div class="submit" @click="submit">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      phone: null,
      code: null,
      flag: true,
      text: "获取验证码",
      num: "",
      time: 30,
    };
  },
  methods: {
    // 发送验证码
    setcode() {
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (!reg.test(this.phone)) {
        this.$toast("请输入正确手机号");
        return;
      }

      for (var i = 0; i < 6; i++) {
        let a = Math.ceil(Math.random() * 9);
        this.num += a;
      }
      this.flag = false;
      this.text = `${this.num}\n有效期${this.time}秒`;
      alert(this.num);

      setTimeout(() => {
        this.flag = true;
        this.text = `获取验证码`;
        this.num = "";
      }, 30000);
    },

    // 登录
    submit() {
      let reg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      let res = /^\d{6}$/;
      console.log("code", this.code);
      console.log("num", this.num);
      if (
        !reg.test(this.phone) ||
        !res.test(this.code) ||
        !(this.num === this.code)
      ) {
        //不正确处理
        this.$toast("请输入正确手机号或验证码");
        return;
      }
      localStorage.setItem("phones",this.phone)
      let frs = this.phone.toString().split("");
      let aa = frs[0] + frs[1] + frs[2];
      let bb = frs[9] + frs[10];
      let cc = aa + "******" + bb;
      window.localStorage.setItem("token", "3e4r5t6yss");
      window.localStorage.setItem("phone", cc);
      window.localStorage.setItem("userId", "376163921");
      this.$toast("登陆成功");
      this.$router.push("/personal");
    },

  },
};
</script>

<style lang="scss" scoped>
.settitle {
  height: 60px;
  display: flex;
  align-items: center;
  background-color: #fff;
  box-shadow: 0px 0px 0px 1px rgba($color: #cccc, $alpha: 0.4);
  .setimg {
    height: 18px;
    position: absolute;
    left: 15px;
    img {
      height: 100%;
      display: block;
    }
  }
  .text {
    margin: 0 auto;
  }
}
.box {
  padding: 25px;
  h2 {
    font-size: 30px;
    font-weight: 700;
  }
}
.form {
  padding: 25px;
  .box {
    display: flex;
    justify-content: center;
    .btn {
      font-size: 20px;
      height: 30px;
      line-height: 30px;
      padding: 10px;
      border: 1px solid rgba(51, 51, 51,0.4);
      border-radius: 5px;
      box-shadow: 0px 0px 8px 2px #cecece;
    }
    .btn.con {
      pointer-events: none;
    }
  }
  .item {
    display: flex;
    height: 60px;
    line-height: 60px;
    margin: 0 auto;
    margin-bottom: 50px;
    padding-bottom: 10px;
    border-bottom: 3px solid #ccc;
    align-items: center;
    &:nth-child(1){
       margin-bottom: 0px;
    }
    &:nth-last-child(1) {
      border-bottom: 0px;
    }
    .phones {
      position: relative;
      &::after {
        content: "";
        display: block;
        position: absolute;
        height: 40px;
        width: 3px;
        background-color: #ccc;
        top: 0px;
        bottom: 0px;
        margin: auto 0;
        right: -10px;
      }
    }
    input {
      background-color: transparent;
      border: 0px;
      outline: none;
      text-indent: 1em;
      font-size: 22px;
      color: #2b2b2b;
    }
    span {
      font-size: 20px;
      margin-left: 10px;
      display: inline-block;
      vertical-align: middle;
    }
    .submit {
      font-size: 20px;
      font-weight: 800;
      width: 300px;
      margin: auto;
      background-color: rgba(255, 255, 255, 0.6);
      box-shadow: 0px 0px 5px 0px #ccc;
      text-align: center;
      color: #333;
      border-radius: 15px;
    }
  }
}
</style>